package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.CraftsmanJobQO;
import com.xingkeduo.pojo.table.CraftsmanJobTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.LeavingTypeEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.RegularUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 一线员工工作信息封装类
 *
 * @author 高杰
 * @time 2019年1月8日 下午5:27:05
 * @description
 */
@Component
public class CraftsmanJobJTO extends BaseJson {
    public static CraftsmanJobTO createTableForModify(String json) throws Throwable {
        CraftsmanJobTO table = new CraftsmanJobTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setLevelId(validLevelId(root, YesNoEnum.NO)).setDutyId(validDutyId(root, YesNoEnum.NO))
                .setNickName(validNickName(root, YesNoEnum.NO)).setContactName(validContactName(root, YesNoEnum.NO))
                .setContactPhone(validContactPhone(root, YesNoEnum.NO));

        return table;
    }

    public static CraftsmanJobQO createQueryForList(String json) throws Throwable {
        CraftsmanJobQO query = new CraftsmanJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setDutyId(validDutyId(root, YesNoEnum.NO)).setShopId(validShopId(root, YesNoEnum.NO))
                .setCityId(validCityId(root, YesNoEnum.NO));
        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            query.setNickName(condition);
        }
        return query;
    }

    public static CraftsmanJobQO createQueryForPrimary(String json) throws Throwable {
        CraftsmanJobQO query = new CraftsmanJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));

        return query;
    }


    public static CraftsmanJobQO createQueryForCondition(String json) throws Throwable {
        CraftsmanJobQO query = new CraftsmanJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            if (condition.matches("\\d+")) {
                query.setId(Long.parseLong(condition));
            } else {
                query.setNickName(condition);
            }

        }

        return query;
    }

    /**
     * 订单外部接口
     *
     * @param json:
     * @return : com.xingkeduo.pojo.query.CraftsmanJobQO
     * @author : gaozifeng
     * @date : 2019/7/5 17:48
     */
    public static CraftsmanJobQO createQueryForConditionByCode(String json) throws Throwable {
        CraftsmanJobQO query = new CraftsmanJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            if (!condition.matches("\\d+")) {
                query.setNickName(condition);
            }
        }
        return query;
    }

    /**
     * 查询code，姓名，艺名
     *
     * @param json:
     * @return : com.xingkeduo.pojo.query.CraftsmanJobQO
     * @author : gaozifeng
     * @date : 2019/5/17 20:33
     */
    public static CraftsmanJobQO createQueryForConditionCode(String json) throws Throwable {
        CraftsmanJobQO query = new CraftsmanJobQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        if (root.has("condition")) {
            String condition = root.path("condition").asText().trim();
            query.setNickName(condition);
            if (condition.matches("\\d+")) {
                query.setCode(Long.parseLong(condition));
            }

        }

        return query;
    }

    public static ObjectNode toJson(CraftsmanJobTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (table != null) {
            if (table.getNickName() != null)
                root.put("nickname", table.getNickName());
            if (table.getContactName() != null)
                root.put("contactname", table.getContactName());
            if (table.getContactPhone() != null)
                root.put("contactphone", table.getContactPhone());
            if (table.getInTime() != null)
                root.put("intime", formatTimestamp.format(table.getInTime()));
            if (table.getFormalTime() != null)
                root.put("formaltime", formatTimestamp.format(table.getFormalTime()));
            if (table.getLevelId() > 0)
                root.put("levelid", table.getLevelId());
            if (table.getDutyId() > 0)
                root.put("dutyid", table.getDutyId());
            if (table.getCityId() > 0)
                root.put("cityid", table.getCityId());
            if (table.getLeaveTime() != null) {
                root.put("leavetime", formatTimestamp.format(table.getLeaveTime()));
            }
            if (table.getGradTime() != null) {
                root.put("gradtime", formatTimestamp.format(table.getGradTime()));
            }
            if (0 != table.getShopId()) {
                root.put("shopId", table.getShopId());
            }
            root.put("companyid", table.getCompanyId());
            toBaseJson(root, table);
        }
        return root;
    }

    public static String validNickName(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "nickname";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static long validLevelId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "level";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isPositiveInteger(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Long.parseLong(value);
        }
        return 0;
    }

    public static long validDutyId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "duty";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (Long.parseLong(value) == 0) {
                return 0;
            }

            if (!RegularUtil.isPositiveInteger(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Long.parseLong(value);
        }
        return 0;
    }

    public static long validShopId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "shop";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (Long.parseLong(value) == 0) {
                return 0;
            }

            if (!RegularUtil.isPositiveInteger(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Long.parseLong(value);
        }
        return 0;
    }

    public static long validCityId(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "city";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (Long.parseLong(value) == 0) {
                return 0;
            }

            if (!RegularUtil.isPositiveInteger(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Long.parseLong(value);
        }
        return 0;
    }

    public static YesNoEnum validIsLeave(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "isleave";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!YesNoEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return YesNoEnum.getYesNo(value);
        }
        return null;
    }

    public static Date validLeaveTime(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "leavetime";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isDateTime(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(value);
        }
        return null;
    }

    public static LeavingTypeEnum validLeaveType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "leavetype";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!LeavingTypeEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return LeavingTypeEnum.getLeaveType(value);
        }
        return null;
    }

    public static String validLeaveRemark(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "leaveremark";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 100)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validContactName(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "contactname";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validContactPhone(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "contactphone";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!RegularUtil.isSelfPhone(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }
}
